﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

namespace KrisThom.Email
{
    static class Send
    {
        public static void CreateMessageWithAttachment(String Server, String Username, String Password, String[] ToField, String FromField, String Subject, String Body, String[] Filepath)
        {
            // Create a message and set up the recipients.
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress(FromField);
                message.Subject = Subject;
                message.Body = Body;

                foreach (String to in ToField)
                {
                    message.To.Add(to);
                }

                foreach (String strFile in Filepath)
                {
                    Attachment data = new Attachment(strFile, MediaTypeNames.Application.Octet);
                    message.Attachments.Add(data);
                }


                //Send the message.
                using (SmtpClient client = new SmtpClient(Server))
                {
                    NetworkCredential creds = new NetworkCredential(Username, Password);
                    // Add credentials
                    client.Credentials = creds;

                    try
                    {
                        client.Send(message);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.Message);
                        throw;
                    }
                } //dispose smtp
            }//dispose mailmessage
        }//end class
    }
}
